package edu.cdbi.database;

import junit.framework.TestCase;
import edu.cdbi.model.instance.Student;

public class StudentManagementTest extends TestCase {
	
	private StudentManagement management;

	protected void setUp() throws Exception {
		super.setUp();
		management = new StudentManagement();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void testInsert() {
		int id = management.insert("name", "mail");
		int counter = 0;
		for(Student student :management.getList()){
			if(student.getId() == id){
				counter++;
				assertTrue(student.getName().equals("name"));
				assertTrue(student.getEmail().equals("mail"));
			}
		}
		assertTrue(counter == 1);
		management.delete(id);
	}

	public void testGetList() {
		/*讀取2筆測試資料，測試搜尋功能是否正確
		 * 分別為
		 * 1. john, xxx@xxx.xxx
		 * 2. eric, zzz@zzz.zzz
		 */
		int counter = 0;
		for(Student student : management.getList()){
			if(student.getName().equals("john") &&
					student.getEmail().equals("xxx@xxx.xxx")){
				counter ++;
			}else if(student.getName().equals("eric") &&
					student.getEmail().equals("zzz@zzz.zzz")){
				counter ++;
			}
		}
		
		assertTrue(counter == 2);
	}

	public void testDelete() {
		/*刪除1筆測試資料，測試刪除功能是否正確
		 * 分別為
		 * 1. 1, ken, test@test.test
		 */
		int counter = 0;
		for(Student student : management.getList()){
			if(student.getId() == 1){
				counter ++;
			}
		}
		
		assertTrue(counter == 1);
		
		management.delete(1);
		for(Student student : management.getList()){
			if(student.getId() == 1){
				counter --;
			}
		}
		
		assertTrue(counter == 1);
	}
}
